
import pandas as pd
import prince
from pylab import mpl

mpl.rcParams["font.sans-serif"] = ["SimHei"]
mpl.rcParams["axes.unicode_minus"] = False

pd.set_option('display.float_format', lambda x: '{:.6f}'.format(x))
X = pd.DataFrame(data=[
    [56, 12, 28, 10, 9],
    [43, 9, 29, 16, 14],
    [43, 17, 38, 12, 11],
    [47, 11, 28, 14, 13],
    [34, 7, 21, 7, 6],
    [64, 12, 35, 16, 15],
    [58, 10, 29, 11, 12],
    [29, 11, 26, 12, 9],
    [63, 7, 17, 13, 10],
    [61, 12, 34, 16, 13],
    [53, 9, 30, 17, 14],
    [33, 10, 26, 10, 8],
    [55, 9, 27, 11, 9],
    [26, 15, 22, 8, 5]],
    columns=pd.Series(['A', 'B', 'C', 'D', 'E']),
    index=pd.Series(['药性温和','见效快','容易吸收','功效全面','可搭配其他药物使用','是家庭常备肠胃药','物有所值','是时尚的品牌','是历史悠久的品牌','是值得信赖的品牌','是专业的品牌','是力求创新的品牌','是亲切的品牌','女性化的品牌'])
)
print(X)

ca = prince.CA(n_components=2,
               n_iter=3,
               copy=True,
               check_input=True,
               engine='auto',
               random_state=42
               )
X.columns.rename('Row', inplace=True)
X.index.rename('Column', inplace=True)
ca = ca.fit(X)

x_row = ca.row_coordinates(X)
print(x_row)

x_column = ca.column_coordinates(X)
print(x_column)

ax = ca.plot_coordinates(X=X,
                         ax=None,
                         figsize=(6, 6),
                         x_component=0,
                         y_component=1,
                         show_row_labels=True,
                         show_col_labels=True
                         )
ax.get_figure().savefig('images/ca_coordinates-2.svg')
